package com.kbtx.studyBrowser;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.kbtx.studyBrowser.URLsupport.GetPostUtil;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* loaded from: classes2.dex */
public class MainActivity extends AppCompatActivity {
    public static final int UPDATE_MAIN_PAGE = 291;
    ListView accountList;
    List<Map<String, String>> accounts;
    SimpleAdapter adapter;
    MyHandler handler = new MyHandler(new WeakReference<>(this));
    JSONArray ja;
    JSONObject jo;
    long t1;

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, androidx.activity.ComponentActivity, androidx.core.app.ComponentActivity, android.app.Activity
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.t1 = System.currentTimeMillis();
        new Thread(new Runnable() { // from class: com.kbtxwer.studyBrowser.-$$Lambda$MainActivity$mr-qPFcu_vUbgNioNh_xgYF5FFU
            @Override // java.lang.Runnable
            public final void run() {
                String str = GetPostUtil.sendGet("https://kbtxwer.gitee.io/edu_addr.json", "");
                Message msg = new Message();
                msg.what = UPDATE_MAIN_PAGE;
                msg.obj = str;
                handler.sendMessage(msg);
            }
        }).start();
        this.accountList = findViewById(R.id.aList);
    }

    /* loaded from: classes2.dex */
    private class MyHandler extends Handler {
        private WeakReference<MainActivity> mainActivity;

        public MyHandler(WeakReference<MainActivity> mainActivity) {
            this.mainActivity = mainActivity;
        }

        @Override // android.os.Handler
        public void handleMessage(Message msg) {
            if (msg.what == UPDATE_MAIN_PAGE) {
                try {
                    int day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
                    ja = new JSONArray(msg.obj.toString());
                    accounts = new ArrayList<>();
                    for (int i = 0; i < MainActivity.this.ja.length(); i++) {
                        MainActivity.this.jo = (JSONObject) MainActivity.this.ja.get(i);
                        String type = jo.getString("type");
                        //星期一到星期五不能展示视频入口
                        if(type.equals("entertainment") && 2<=day && day<=6 ) continue;
                        Map<String, String> map = new HashMap<>();
                        map.put("accountName", jo.getString("name"));
                        map.put("basicURL", jo.getString("url"));
                        accounts.add(map);
                    }
                } catch (JSONException e) {
                    MainActivity mainActivity = MainActivity.this;
                    Toast.makeText(mainActivity, "错误，当前字符串无法解析为json，具体内容为：" + msg.obj.toString(), Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                    try {
                        Thread.sleep(1000L);
                        MainActivity.this.finish();
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
                MainActivity mainActivity2 = MainActivity.this;
                Toast.makeText(mainActivity2, "准备就绪，耗时" + (System.currentTimeMillis() - MainActivity.this.t1) + "毫秒", Toast.LENGTH_SHORT).show();
                MainActivity mainActivity3 = MainActivity.this;
                mainActivity3.adapter = new SimpleAdapter(mainActivity3, mainActivity3.accounts, R.layout.article_provider_list, new String[]{"accountName", "basicURL"}, new int[]{R.id.account, R.id.basicURL});
                MainActivity.this.accountList.setAdapter(MainActivity.this.adapter);
                MainActivity.this.accountList.setOnItemClickListener(new AdapterView.OnItemClickListener() { // from class: com.kbtxwer.studyBrowser.-$$Lambda$MainActivity$MyHandler$LpAavyPPC1FdUzuH2oVtn8ZywlM
                    @Override // android.widget.AdapterView.OnItemClickListener
                    public final void onItemClick(AdapterView adapterView, View view, int i2, long j) {
                        Intent intent = new Intent(MainActivity.this, BrowseActivity.class);
                        intent.putExtra("viewAddress", ((TextView) view.findViewById(R.id.basicURL)).getText().toString());
                        intent.putExtra("title", ((TextView) view.findViewById(R.id.account)).getText().toString());
                        MainActivity.this.startActivity(intent);
                    }
                });
            }
        }
    }
}
